﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.ProcessManager.Server.Process.Security;
using Sinacor.Infra.Core.ProcessManager.Server.Security;
using Sinacor.Infra.Service.Common;
using Sinacor.Infra.Service.Security.Services;
using Sinacor.Process.Bovespa.Financeiro.Server.Bovespa.Cliente.Cliente;

namespace Sinacor.Process.Bovespa.Financeiro.Server.Communication
{
    public class Cliente : BaseCommunication
    {
        #region Construtor

        public Cliente()
            : base(AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.LoginName, 
                   AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.Password, 
                   AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.CompanyId)
        {
        }

        #endregion

        #region Metodos

        public ProcessEntity.Cliente ObterCliente(ProcessEntity.Parameter.FiltroCliente model)
        {
            ProcessEntity.Cliente retorno = null;
            ICliente servico = null;
            Translator.DataContractToProcessEntity.Cliente translatorResponse;
            Translator.ProcessEntityToDataContract.Cliente translatorRequest;

            try
            {
                using (ServiceInstance<ICliente> service = new ServiceInstance<ICliente>())
                {
                    servico = service.TryGetInstance(this.LoginName, this.Password, this.CompanyId);
                    translatorRequest = new Translator.ProcessEntityToDataContract.Cliente();
                    translatorResponse = new Translator.DataContractToProcessEntity.Cliente();

                    retorno  = translatorResponse.ToCliente(servico.ObterCliente(translatorRequest.ToFiltroClienteRequest(model)));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

            return retorno;
        }

        #endregion
    }
}
